# TOP Makefile

BUILD_INFO := =============== Build OK!! Author: JinYiCheng  ===============

target = test

executable_suffix := .exe

# top Makefile directory
makefile_dir = $(CURDIR)

# debug build?
build_for_debug = 1

# build for ARM?
build_for_arm = 0

# designate gcc path when without env
# gcc_path ?= F:/GNUtools/GNUtools/gcc-arm-none-eabi-10.3-2021.10

# cross compiler prefix name
ifneq ($(build_for_arm),0)
cross_compile ?= arm-linux-gnueabihf-
endif

# Build path
build_dir = $(makefile_dir)/build

# lib path
lib_dir = lib

# input encode type GB2312 or UTF-8
input_encode = -finput-charset=UTF-8

# output encode type GB2312 or UTF-8
ouptput_encode = -fexec-charset=UTF-8

# The gcc compiler bin path can be either defined in make command via GCC_PATH variable (> make GCC_PATH=xxx)
ifdef gcc_path
CC = $(gcc_path)/$(cross_compile)gcc
CXX = $(gcc_path)/$(cross_compile)g++
CP = $(gcc_path)/$(cross_compile)objcopy
OD = $(gcc_path)/$(cross_compile)objdump
LD = $(gcc_path)/$(cross_compile)ld
AR = $(gcc_path)/$(cross_compile)ar
else
CC = $(cross_compile)gcc
CXX = $(cross_compile)g++
LD = $(cross_compile)ld
AR = $(cross_compile)ar
endif

obj_files ?= $(patsubst %.c,$(build_dir)/%.o,$(notdir $(c_srcfiles)))
obj_files += $(patsubst %.s,$(build_dir)/%.o,$(notdir $(asm_srcfiles)))

# ASM files directory
asm_dir := 

# C source files directory
csrc_dir := $(makefile_dir)/elNET/os_interface\
			$(makefile_dir)/elNET/src\
			$(makefile_dir)
include_dir := $(makefile_dir)/elNET/inc\
			$(makefile_dir)/elNET/os_interface\
			$(makefile_dir)/elNET/src\
			$(makefile_dir)/elNET/port/Include\
			$(makefile_dir)/elNET/port/Include/pcap\
			$(makefile_dir)/elNET/components/cjson\
			$(makefile_dir)/elNET
			
link_library_dir := $(makefile_dir)/elNET/port/Lib/x64
link_library := wpcap\
				Packet
				
link_path ?= $(addprefix -L,$(link_library_dir))
link_flags := $(link_path) $(addprefix -l,$(link_library))

ifneq ($(build_for_debug),0)
c_flags += -g -O0
else
c_flags += -O2
endif

c_srcfiles := $(foreach dir, $(csrc_dir), $(wildcard $(dir)/*.c))
#search C files in these dirs
vpath %.c $(sort $(dir $(c_srcfiles)))

asm_srcfiles ?= $(foreach dir, $(asm_dir), $(wildcard $(dir)/*.s))
asm_srcfiles += $(foreach dir, $(asm_dir), $(wildcard $(dir)/*.S))
#search ASM files in these dirs
vpath %.s $(sort $(dir $(asm_srcfiles)))

c_flags += $(ouptput_encode) $(input_encode)

# add c file header path
c_flags += $(addprefix -I,$(include_dir))

s_flags := $(c_flags) -x assembler-with-cpp

all : $(build_dir) $(build_dir)/$(target) debug_info

#非交叉编译一般不需要链接脚本
link_script := 

$(build_dir)/$(target) : $(obj_files)
	$(CC) -v $(link_script) $^ -o $@ $(link_flags)
	size $(build_dir)/$(target)$(executable_suffix)

$(build_dir)/%.o : %.c
	$(CC) -Wall $(c_flags) -c $< -o $@

$(build_dir)/%.o : %.s
	$(CC) -Wall $(s_flags) -c $< -o $@

$(build_dir) :
	mkdir $@

$(lib_dir) :
	mkdir $@

debug_info:
	@echo $(BUILD_INFO)

.PHONY: clean echo

echo:
	@echo $(obj_files)
	@echo $(build_dir)
	@echo $(c_srcfiles)
	@echo $(include_dir)
	@echo $(c_flags)
	@echo $(s_flags)
	@echo $(link_flags)

clean:
	-rm -fr $(build_dir)/*.o $(build_dir)/*.coff $(build_dir)/*.exe